import routes from 'server/route/index';
import 'server/route/loginRoute';
import 'server/route/commonRoute';
import 'server/route/playerRoute';
import 'server/route/cardRoute';
import 'server/route/normalRoute';
import 'server/route/cheatRoute';
import playerStore from 'js/store/playerStore';
import utils from 'server/core/utils';

let routesHandle = (url, data, cb) => {
    let msg = data;

    let notLoginRoute = routes.notLogin[url];
    if (notLoginRoute) {
        let validationData = routes.validationData[url];
        if (validationData) {
            let ret = utils.verification(msg, validationData);
            if (ret) {
                cb({
                    err: 102,
                    data: ret
                });
                return;
            }
            return;
        }
        notLoginRoute(msg, (err, data) => {
            cb({
                err: err,
                data: data
            });
        });
        return;
    }

    let userLoginRoute = routes.userLogin[url];
    if (userLoginRoute) {
        let validationData = routes.validationData[url];
        if (validationData) {
            let ret = utils.verification(msg, validationData);
            if (ret) {
                cb({
                    err: 102,
                    data: ret
                });
                return;
            }
            return;
        }
        userLoginRoute(msg, (err, data) => {
            cb({
                err: err,
                data: data
            });
        });
        return;
    }

    let loginRoute = routes.login[url];
    if (loginRoute) {
        let validationData = routes.validationData[url];
        if (validationData) {
            let ret = utils.verification(msg, validationData);
            if (ret) {
                cb({
                    err: 102,
                    data: ret
                });
                return;
            }

        }
        let player = playerStore.player;
        loginRoute(player, msg, (err, data) => {
            cb({
                err: err,
                data: data
            });
        });
        return;
    }

    cb({
        err: 103,
        data: {
            url: url,
            routes: routes
        }
    });

};

export default routesHandle;